package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ReadFeedbackMapper;
import com.example.demo.Mapper.unReadFeedbackMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.Commodity;
import com.example.demo.entity.ReadFeedback;
import com.example.demo.entity.unReadFeedback;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/unReadFeedback")
public class unReadFeedbackController {
    @Resource
    unReadFeedbackMapper unreadFeedbackMapper;
    @Resource
    ReadFeedbackMapper readFeedbackMapper;

    /**
     * 查询全部信息  √
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll(@RequestParam String tenantId) {
        QueryWrapper<unReadFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        return Result.success(unreadFeedbackMapper.selectList(null));
    }

    /**
     * 分页查询未读反馈信息中所有数据 √
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageU(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<unReadFeedback> wrapper = Wrappers.<unReadFeedback>lambdaQuery();
        wrapper.eq(unReadFeedback::getTenantId, Integer.valueOf(tenantId));
        Page<unReadFeedback> unReadFeedbackPage = unreadFeedbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(unReadFeedbackPage);
    }

    /**
     * 新增   √
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveU(@RequestParam String name,
                           @RequestParam String sex,
                           @RequestParam String identify,
                           @RequestParam String tel,
                           @RequestParam String email,
                           @RequestParam String address,
                           @RequestParam String productCode,
                           @RequestParam String productName,
                           @RequestParam String feedbackInfo,
                           @RequestParam String tenantId) {
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        unReadFeedback unRead = new unReadFeedback();
        unRead.setName(name);
        unRead.setSex(sex);
        unRead.setIdentify(identify);
        unRead.setTel(tel);
        unRead.setEmail(email);
        unRead.setAddress(address);
        unRead.setProductCode(productCode);
        unRead.setProductName(productName);
        unRead.setFeedbackInfo(feedbackInfo);
        unRead.setTenantId(Integer.valueOf(tenantId));
        unreadFeedbackMapper.insert(unRead);
        return Result.success();
    }

    /**
     * 根据identify更新  √
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */

    @GetMapping(value = "/update")
    public Result<?> updateU(@RequestParam String name,
                             @RequestParam String sex,
                             @RequestParam String identify,
                             @RequestParam String tel,
                             @RequestParam String email,
                             @RequestParam String address,
                             @RequestParam String productCode,
                             @RequestParam String productName,
                             @RequestParam String feedbackInfo,
                             @RequestParam String tenantId) {
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        unReadFeedback unRead = new unReadFeedback();
        unRead.setName(name);
        unRead.setSex(sex);
        unRead.setIdentify(identify);
        unRead.setTel(tel);
        unRead.setEmail(email);
        unRead.setAddress(address);
        unRead.setProductCode(productCode);
        unRead.setProductName(productName);
        unRead.setFeedbackInfo(feedbackInfo);
        unRead.setTenantId(Integer.valueOf(tenantId));
        unreadFeedbackMapper.updateByMultiId(unRead);
        return Result.success();
    }

    /**
     * 根据身份证查询信息  √
     *
     * @param identify
     * @return
     */
    @GetMapping("/findById")
    public Result<?> findById(@RequestParam String identify,
                                  @RequestParam String tenantId) {
        unReadFeedback unRead = new unReadFeedback();
        unRead.setTenantId(Integer.valueOf(tenantId));
        unRead.setIdentify(identify);
        unReadFeedback tmp = unreadFeedbackMapper.selectByMultiId(unRead);
        List<unReadFeedback> unReadFeedbackInfoList = new ArrayList<>();
        if (tmp != null) {
            unReadFeedbackInfoList.add(tmp);
            return Result.success(unReadFeedbackInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 用户查询反馈记录
     * @param identify
     * @param name
     * @return
     */

    @GetMapping("/findOne")
    public Result<?> findOne(@RequestParam String identify,
                             @RequestParam String name) {
        LambdaQueryWrapper<unReadFeedback> wrapper = Wrappers.<unReadFeedback>lambdaQuery();
        wrapper.eq(unReadFeedback::getIdentify, identify).eq(unReadFeedback::getName, name);
        unReadFeedback unRead = unreadFeedbackMapper.selectOne(wrapper);
        if (unRead != null) {
            return Result.success(unRead);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据name模糊查询  √
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<?> findByName(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam String name,
                                @RequestParam String tenantId) {
        LambdaQueryWrapper<unReadFeedback> wrapper = Wrappers.<unReadFeedback>lambdaQuery();
        wrapper.like(unReadFeedback::getName, name)
                .eq(unReadFeedback::getTenantId, tenantId);
        Page<unReadFeedback> unReadFeedbackPage = unreadFeedbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (unReadFeedbackPage.getTotal() == 0)
            return Result.error(0, "没有查询到相关数据");
        return Result.success(unReadFeedbackPage);
    }

    /**
     * 根据身份证删除信息  √
     *
     * @param identify
     * @return
     */

    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String identify,
                            @RequestParam String tenantId) {
        unReadFeedback unRead = new unReadFeedback();
        unRead.setTenantId(Integer.valueOf(tenantId));
        unRead.setIdentify(identify);
        int res = unreadFeedbackMapper.deleteByMultiId(unRead);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 根据id来将信息读取并插入已读表   × 需要接口联调
     *
     * @param identify
     * @return
     */
    @GetMapping("/read")
    public Result<?> read(@RequestParam String identify,
                          @RequestParam String tenantId) {
        unReadFeedback tmp = new unReadFeedback();
        tmp.setTenantId(Integer.valueOf(tenantId));
        tmp.setIdentify(identify);
        unReadFeedback unread = unreadFeedbackMapper.selectByMultiId(tmp);
        ReadFeedback read = new ReadFeedback();
        read.setName(unread.getName());
        read.setSex(unread.getSex());
        read.setIdentify(unread.getIdentify());
        read.setTel(unread.getTel());
        read.setEmail(unread.getEmail());
        read.setAddress(unread.getAddress());
        read.setProductCode(unread.getProductCode());
        read.setProductName(unread.getProductName());
        read.setFeedbackInfo(unread.getFeedbackInfo());
        read.setTenantId(unread.getTenantId());
        readFeedbackMapper.insert(read);
        unreadFeedbackMapper.deleteByMultiId(tmp);
        return Result.success();
    }

}

